﻿layui.config({
    base: "Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		pageData = { "PageSize": 10, "PageCount": 0, "PageNumber": 0, "RowCount": 0,"Where":"" };

    //表格数据和分页
    function getList(that) {
        
        $.ajax({
            type: "POST",
            url: "/TasksManagement/Tasks_Show",
            data: that,
            dataType: "json",
            success: function (data) {
                if (data.State == 0) {
                    layer.alert(data.Message, { icon: 7 });
                } else
                {
                    var result = JSON.parse(data.Data);
                    var usersData = result.DataList;
                    pageData.PageCount = result.PageCount;
                    pageData.PageNumber = result.PageNumber;
                    pageData.PageSize = result.PageSize;
                    pageData.RowCount = result.RowCount;
                    renderDate(usersData);
                    showPage(pageData);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert("123");
            }
         
        });
    };
    //渲染数据
    function renderDate(list) {
        var dataHtml = '';
           
        if (list != null) {
            layui.each(list, function (index, item) {
                if (item.State == false) {
                        
                    item.State = "未完成";
                } else {
                    item.State = "已完成";
                }
                dataHtml += '<tr>'
                + '<td>' + item.Id + '</td>'
                + '<td align="left">' + item.TaskName + '</td>'
                + '<td>' + item.BeginTime.substring(0, 10) + '</td>'
                 + '<td>' + item.Originator + '</td>'
                  + '<td>' + item.Participant + '</td>'
                   + '<td>' + item.TaskTypeName + '</td>'
                   + '<td>' + item.State + '</td>'
                   + '<td>' + '<a class="layui-btn" id="details" >详情</a>' 
                    + '<a class="layui-btn" id="Update">修改</a>'
                  + '<a class="layui-btn" id="Removetask">删除</a>' + '</td>'
                + '</tr>';
            });
        } else {
            dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
        }
        $(".news_content").html(dataHtml);   //渲染数据
        $('.news_list thead input[type="checkbox"]').prop("checked", false);
        form.render(); //渲染表单
    }
    //分页
    function showPage(page) {
        laypage.render({
            elem: 'page',
            count: page.PageCount,//得到总页数
            limit: page.PageSize,
            curr: page.PageNumber + 1,
            groups: 5, //连续显示分页数
            jump: function (obj, first) {
                if (!first) {//防止无限刷新
                    page.PageNumber = obj.curr - 1;
                    getList(page);
                }
            }
        });
    }
    var active = {
        setTop: function (Aid) {
            var that = this;
            $.ajax({
                type: "POST",
                url: "/TasksManagement/Particulars?id="+Aid+"",
                success: function (data) {
                    var result = JSON.parse(data.Data);
                    if (result.State == true) {
                        result.State = "已完成";
                    } else {
                        result.State = "未完成";
                    }
                    if (result.Result==null) {
                        result.Result = "";
                    }
                    if (result.Remark==null) {
                        result.Remark = "";
                    }
                    $("#TaskName").text(result.TaskName);
                    $("#AddTime").text(result.AddTime);
                    $("#BeginTime").text(result.BeginTime);
                    $("#Originator").text(result.Originator);
                    $("#Participant").text(result.Participant);
                    $("#PendTime").text( result.PendTime);
                    $("#Remark").text(result.Remark);
                    $("#Result").text(result.Result);
                    $("#State").text(result.State);
                    $("#TaskTypeName").text(result.TaskTypeName);
                    //多窗口模式，层叠置顶
                    layer.open({
                        type: 1,
                        title: "任务详情",
                        skin: "layui-layer-rim",
                        area: ["50%"],
                        content: $("#test").html()
                    });
                    /* 渲染表单 */
                    form.render();
                }
            });
            

        },
        remove: function (Aid) {
            layer.confirm('确定删除此信息？', { icon: 3, title: '提示信息' }, function (index) {
                var index = layer.msg('删除中，请稍候', { icon: 16, time: false, shade: 0.8 });
                $.ajax({
                    type: "POST",
                    url: "/TasksManagement/Removetask?id=" + Aid + "",
                })
                layer.close(index);
            });
           
        },
        Update: function (Aid) {
            location.href="/TasksManagement/Edit?id=" + Aid + "";
            
        }
         
    }
    $(function () {
       
        getList(pageData);
        $(document).on("click", "#details", function () {
            var _this = $(this).parent().parent();
            Aid = _this.children('td').eq(0).text();
            active.setTop(Aid);
        });
        $(document).on("click", "#Removetask", function () {
            var _this = $(this).parent().parent();
            Aid = _this.children('td').eq(0).text();
            active.remove(Aid);
            getList(pageData);
        });
        $(document).on("click", "#Update", function () {
            var _this = $(this).parent().parent();
            Aid = _this.children('td').eq(0).text();
            active.Update(Aid);
        });

    });
    form.on('submit(Select)', function (data) {
        var where = JSON.stringify(data.field);
        pageData.Where = where;
        getList(pageData);
        return false;
    });
})